package com.kvw.filesystem.util;

import com.kvw.filesystem.exception.PropertyException;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Desc:
 *
 * @author: keen
 * Date: 2020-07-18
 * Time: 13:29
 */
@Slf4j
public final class PropertyUtils {
    private static final String EMPTY_STRING = "";

    public static Properties load(String filePath) {
        try {
            return load(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            throw new PropertyException("配置文件不存在");
        }
    }

    public static Properties load(InputStream is) {
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new PropertyException(e.getMessage(), e);
        }
        return properties;
    }

    public static String getString(Properties properties, String key) {
        return getString(properties, key, EMPTY_STRING);
    }

    public static String getString(Properties properties, String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    public static Double getDouble(Properties properties, String key) {
        return getDouble(properties, key, null);
    }

    public static Double getDouble(Properties properties, String key, Double defaultValue) {
        String val = properties.getProperty(key);
        try {
            return Double.parseDouble(val);
        } catch (NumberFormatException e) {
            log.warn("错误的属性值", e);
        }
        return defaultValue;
    }

    public static Long getLong(Properties properties, String key) {
        return getLong(properties, key, null);
    }

    public static Long getLong(Properties properties, String key, Long defaultValue) {
        String val = properties.getProperty(key);
        try {
            return Long.parseLong(val);
        } catch (NumberFormatException e) {
            log.warn("错误的属性值", e);
        }
        return defaultValue;
    }

    public static Float getFloat(Properties properties, String key) {
        return getFloat(properties, key);
    }

    public static Float getFloat(Properties properties, String key, Float defaultValue) {
        String val = properties.getProperty(key);
        try {
            return Float.parseFloat(val);
        } catch (NumberFormatException e) {
            log.warn("错误的属性值", e);
        }
        return defaultValue;
    }

    public static Integer getInteger(Properties properties, String key) {
        return getInteger(properties, key, null);
    }

    public static Integer getInteger(Properties properties, String key, Integer defaultValue) {
        String val = properties.getProperty(key);
        try {
            return Integer.parseInt(val);
        } catch (NumberFormatException e) {
            log.warn("错误的属性值", e);
        }
        return defaultValue;
    }

    public static Short getShort(Properties properties, String key) {
        return getShort(properties, key, null);
    }


    public static Short getShort(Properties properties, String key, Short defaultValue) {
        String val = properties.getProperty(key);
        try {
            return Short.parseShort(val);
        } catch (NumberFormatException e) {
            log.warn("错误的属性值", e);
        }
        return defaultValue;
    }

    public static Character getCharacter(Properties properties, String key) {
        return getCharacter(properties, key, null);
    }


    public static Character getCharacter(Properties properties, String key, Character defaultValue) {
        String val = properties.getProperty(key);
        if (val.length() == 1) {
            return val.charAt(0);
        } else {
            return defaultValue;
        }
    }


    public static Byte getByte(Properties properties, String key) {
        return getByte(properties, key, null);
    }

    public static Byte getByte(Properties properties, String key, Byte defaultValue) {
        String val = properties.getProperty(key);
        try {
            return Byte.parseByte(val);
        } catch (NumberFormatException e) {
            log.warn("错误的属性值", e);
        }
        return defaultValue;
    }

    public static Boolean getBoolean(Properties properties, String key) {
        return getBoolean(properties, key, null);
    }

    public static Boolean getBoolean(Properties properties, String key, Boolean defaultValue) {
        String val = properties.getProperty(key);
        if (Boolean.TRUE.toString().equals(val)) {
            return Boolean.TRUE;
        } else if (Boolean.FALSE.toString().equals(val)) {
            return Boolean.FALSE;
        } else {
            return defaultValue;
        }
    }
}
